
package uestc.bitman.edxapp.mainui;

import uestc.bitman.edxapp.R;

import android.app.Activity;
import android.widget.Toast;

public abstract class PressAgainExitController implements IActiviyController{

    private boolean mExit = false;

    private boolean mExitSwitch = true;

    private Activity mActivity;

    public void initController(Activity context) {
        mActivity = context;
    }

    public boolean onBackPressed() {

        if (mExitSwitch) {
            if (mExit) {
                return false;
            } else {
                Toast.makeText(mActivity, mActivity.getString(R.string.press_again_exit),
                        Toast.LENGTH_SHORT).show();
                new Thread(new TickerThread()).start();
                return true;
            }
        } else {
            handleBackPress();
            return false;
        }
    }

    public void enablePressAgainToExit(boolean b) {
        mExitSwitch = b;
    }

    class TickerThread implements Runnable {
        @Override
        public void run() {
            mExit = true;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mExit = false;
        }
    }

    public abstract void handleBackPress();
}
